package com.sscaipiao.android.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.sscaipiao.android.R;

/**
 * Created by Airay.Rong on 13-12-30.
 */
public class BallBase extends LinearLayout implements View.OnClickListener {
	protected TextView mLabel;
	protected Button mButton;
	protected boolean bSelect = false;
	protected boolean bHideLabel = false;
	protected int picID;

	public BallBase(Context context) {
		super(context);
	}

	public BallBase(Context context, AttributeSet attrs) {
		super(context, attrs);
		setOrientation(HORIZONTAL);
		TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Ball);
		CharSequence labelText = ta.getText(R.styleable.Ball_label);
		CharSequence textText = ta.getText(R.styleable.Ball_text);
		int labelColor = ta.getColor(R.styleable.Ball_labelColor, Color.RED);
		int textColor = ta.getColor(R.styleable.Ball_textColor, Color.BLACK);
		int labelSize = ta.getInteger(R.styleable.Ball_labelSize,30);
		bSelect = ta.getBoolean(R.styleable.Ball_select, false);
		bHideLabel = ta.getBoolean(R.styleable.Ball_hidelabel, false);
		ta.recycle();

		LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		li.inflate(R.layout.redball,this,true);
		mLabel = (TextView) findViewById(R.id.label);
		mButton = (Button) findViewById(R.id.ball);
		mButton.setOnClickListener(this);
		if (labelText != null) {
			mLabel.setText(labelText);
		}
		if (textText != null) {
			mButton.setText(textText);
		}
		if (bHideLabel == true) {
			hideLabel();
		}
	}

	private onClickCallback mCallback ;

	@Override
	public void onClick(View v) {
		if (mCallback != null) {
			bSelect = !bSelect;
			changeUI();
			mCallback.onBallClick(this);
		}
	}

	public interface onClickCallback{
		void onBallClick(View me);
	}
	public void setLabel(CharSequence t){
		mLabel.setText(t);
	}
	public void setLabel (int resid){
		setLabel(getContext().getResources().getText(resid));
	}
	public void setText(CharSequence t){
		mButton.setText(t);
	}

	public String getBallText() {
		return mButton.getText().toString();
	}

	public void setPicID(int id) {
		picID = id;
		changeUI();
	}
	public void setBackground(int id) {
		mButton.setBackgroundDrawable(getResources().getDrawable(id));
	}

	private void changeUI() {
		if (bSelect == true) {
			mButton.setBackgroundDrawable(getResources().getDrawable(picID));
			mButton.setTextColor(Color.WHITE);
		} else {
			mButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.double_ball_unchoose));
			mButton.setTextColor(Color.RED);
		}
	}

	public TextView getLabel() {
		return mLabel;
	}

	public Button getButton() {
		return mButton;
	}


	public CharSequence getText() {
		return mButton.getText();
	}

	public void reset() {
		bSelect = false;
		mButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.double_ball_unchoose));
		mButton.setTextColor(Color.RED);
	}
	public void setSelected(boolean select) {
		bSelect = select;
		changeUI();
	}

	public boolean isSelect() {
		return bSelect;
	}

	public void setCallback(onClickCallback cb) {
		mCallback = cb;
	}

	public void hideLabel() {
		mLabel.setVisibility(View.GONE);
	}

}
